package com.zzld.modules.autoinn.action;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.Base64Coder;
import com.zzld.core.utils.CookieUtils;
import com.zzld.core.utils.GeoUtil;
import com.zzld.core.utils.MessageUtils;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Area;
import com.zzld.modules.autoinn.entity.Collar;
import com.zzld.modules.autoinn.entity.Collect;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.Store;
import com.zzld.modules.autoinn.service.IAreaService;
import com.zzld.modules.autoinn.service.ICollectService;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.autoinn.service.IProductPingService;
import com.zzld.modules.autoinn.service.IStoreService;
import com.zzld.modules.common.dto.AreaSelect;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.common.dto.NavCat;
import com.zzld.modules.common.dto.NavTab;
import com.zzld.modules.common.dto.SortSelect;

@Controller
@RequestMapping("/weixin/f/autoinn/xc/collect")
public class WXCollectController {

	@Autowired
	private ICollectService collectService;

	@Autowired
	private IProductPingService productPingService;

	@Autowired
	private ICustomerService customerService;

	private String listurl = "/wx/autoinn/xc_store_colect";

	@RequestMapping(method = RequestMethod.GET)
	public String liq() {
		return listurl;
	}

	@RequestMapping("/pagelist")
	@ResponseBody
	public CommonJSON wxxcstore(Model model, HttpServletRequest request) {
		CommonJSON result = new CommonJSON();
		Map<String, Object> map = new HashMap<String, Object>();
		// 区域ID
		String area_f = request.getParameter("area_f");
		String area_sub = request.getParameter("area_sub");

		if (area_f != null && area_f.equals("null")) {
			area_f = null;
		}

		if (area_sub != null && area_sub.equals("null")) {
			area_sub = null;
		}

		map.put("area_f", area_f);
		map.put("area_sub", area_sub);

		// lng经度,lat纬度
		String lng = request.getParameter("lng") == null ? "0" : request
				.getParameter("lng").toString();

		String lat = request.getParameter("lat") == null ? "0" : request
				.getParameter("lat").toString();

		Integer pageNum = Integer
				.parseInt(request.getParameter("pageNum") == null ? "1"
						: request.getParameter("pageNum"));

		double longitude = Double.parseDouble(lng);
		double latitude = Double.parseDouble(lat);

		// 获取排序
		String sort = request.getParameter("sort") == null ? "jlzj" : request
				.getParameter("sort");
		if (sort.equals("null")) {
			sort = "jlzj";
		}
		map.put("lat", latitude);
		map.put("lng", longitude);

		try {

			String openid = CookieUtils.getCookie(request, "autoinn_session");
			String wxaccount = CookieUtils.getCookie(request, "wx_session");

			Customer customer = null;

			openid = Base64Coder.decode(openid);
			wxaccount = Base64Coder.decode(wxaccount);
			customer = customerService.findCustomer(wxaccount, openid);

			map.put("openid", openid);
			map.put("customerId", customer.getCustomerId());

			PageInfo<Store> storeList = collectService.selectOrderDistance(map,
					pageNum, 10);
			result.setSuccess(true);
			// 总条数
			result.setTotal(storeList.getTotal());
			result.setRows(storeList.getList());
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
		}

		return result;

	}

	public int fen(String storeid) {
		// 参与评论人数
		int counts = productPingService.selectbycount(storeid);
		// 评论总分数
		int fe = productPingService.selectbysum(storeid);

		if (counts == 0 || fe == 0) {
			return 0;
		}
		double q = fe / counts;

		BigDecimal bd = new BigDecimal(q);
		bd = bd.setScale(0, BigDecimal.ROUND_HALF_UP);

		return Integer.parseInt(bd.toString());
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON add(Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		// String openid="123";
		// String wxaccount="12312321";
		String openid = CookieUtils.getCookie(request, "autoinn_session");
		String wxaccount = CookieUtils.getCookie(request, "wx_session");

		Customer customer = null;
		try {
			openid = Base64Coder.decode(openid);
			wxaccount = Base64Coder.decode(wxaccount);
			customer = customerService.findCustomer(wxaccount, openid);

		} catch (Exception e1) {
			e1.printStackTrace();
		}

		Collect entity = new Collect();
		entity.setCollectId(UuidUtil.uuid().toString());
		entity.setCollectState(0);
		entity.setOpenid(openid);
		entity.setCustomerId(customer.getCustomerId());
		entity.setId(request.getParameter("storeid") == null ? "" : request
				.getParameter("storeid"));
		entity.setCreateData(new Date());

		try {

			if (collectService.save(entity) > 0) {
				json.setMessage("收藏成功");
				json.setSuccess(true);
			}
		} catch (Exception e) {
			json.setMessage(MessageUtils.ADD_FAIL);
			json.setSuccess(false);
		}
		
		return json;
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(Model model, HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		// String openid="123";
		// String wxaccount="12s312321";
		String openid = CookieUtils.getCookie(request, "autoinn_session");
		String wxaccount = CookieUtils.getCookie(request, "wx_session");

		Customer customer = null;
		try {
			openid = Base64Coder.decode(openid);
			wxaccount = Base64Coder.decode(wxaccount);
			customer = customerService.findCustomer(wxaccount, openid);

		} catch (Exception e1) {
			e1.printStackTrace();
		}

		String id = request.getParameter("delid");
		try {
			Example eq = new Example(Collect.class);
			eq.createCriteria().andEqualTo("id", id)
					.andEqualTo("openid", openid)
					.andEqualTo("customerId", customer.getCustomerId());
			String cid = collectService.selectByExample(eq).get(0)
					.getCollectId();

			if (collectService.deleteByPrimaryKey(cid) > 0) {
				json.setMessage("已取消收藏");
				json.setSuccess(true);
			}
			Example e = new Example(Collect.class);
			e.createCriteria().andEqualTo("openid", openid);

			try {
				List<Collect> c = collectService.selectByExample(e);
				if (collectService.selectByExample(e).size() > 0) {
					json.setTotal((long) 1);
				} else {
					json.setTotal((long) 0);
				}
			} catch (Exception e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
		} catch (Exception e) {
			json.setMessage(MessageUtils.DELETE_ALL_FAIL);
			json.setSuccess(false);
		}

		return json;
	}

}
